from common import BeautySoupTool
import requests
from common import DoConfig
import time
import logging
from common import DateTool
session = requests.session()
logger = logging.getLogger(__name__)


def do_login():
    login_url = 'http://exam.chinapmp.cn/App_Ajax/SHOW.Ajax.Exam.Login,SHOW.Ajax.ajax?id =3&doing=view&domain=exam'

    cn_loginName = config.get_strValue('CN-PMI', 'LOGIN_NAME')
    cn_pwd = config.get_strValue('CN-PMI', 'PASSWORD')
    JSON_DATA = [
        {
            "Name": "uType",
            "Value": 0
        },
        {
            "Name": "uName",
            "Value": cn_loginName
        },
        {
            "Name": "uPass",
            "Value": cn_pwd
        }
    ]
    login_res = session.post(login_url, {'JSON_DATA': JSON_DATA})
    return login_res


# 访问报名页面
def do_register():
    # 登录成功使用以下报名
    register_url = 'http://user.chinapmp.cn/examsign;sign.shtml'
    return session.get(register_url, timeout=(20, 10))


# 保存报名数据
def post_data():
    register_data = [
        {
            "Name": "Ed",
            "Value": 10000044
        },
        {
            "Name": "Etitle",
            "Value": " 2021年6月20日项目管理资格认证考试"
        },
        {
            "Name": "Stype",
            "Value": 101
        },
        {
            "Name": "StypeName",
            "Value": "项目管理师(PMP®)"
        },
        {
            "Name": "Xing",
            "Value": FIRST_NAME
        },
        {
            "Name": "Zhong",
            "Value": ""
        },
        {
            "Name": "Ming",
            "Value": LAST_NAME
        },
        {
            "Name": "Peixunjigou",
            "Value": "515"
        },
        {
            "Name": "Peixunjigouming",
            "Value": "北京光环致成国际管理咨询股份有限公司"
        },
        {
            "Name": "PMIUname",
            "Value": USER_NAME
        },
        {
            "Name": "PMIUpass",
            "Value": PASSWORD
        },
        {
            "Name": "IsPMIUser",
            "Value": false
        },
        {
            "Name": "PMINumber",
            "Value": ""
        },
        {
            "Name": "_PMIUtimeB",
            "Value": ""
        },
        {
            "Name": "_PMIUtimeE",
            "Value": ""
        },
        {
            "Name": "PMItimeB",
            "Value": "/Date(" + time_format(START_TIME) + "+0800)/"
        },
        {
            "Name": "PMItimeE",
            "Value": "/Date(" + time_format(END_TIME) + "+0800)/"
        },
        {
            "Name": "Kaodian",
            "Value": "66"
        },
        {
            "Name": "Kaodianming",
            "Value": "广州才聚-天河区（私立华联学院）"
        },
        {
            "Name": "PMIID",
            "Value": PMI_ID
        }
    ]
    url = ''
    session_post = session.post(url, {'JSON_DATA': register_data})
    return session_post


def time_format(time_str):
    strptime = time.strptime(time_str, '%Y-%m-%d')
    timestamp = int(time.mktime(strptime))
    while len(timestamp) < 13:
        timestamp += '0'
    return timestamp


if __name__ == '__main__':
    config = DoConfig.DoConfig('config.ini')

    PMI_ID = config.get_strValue('PMI', 'PMI_ID')
    START_TIME = config.get_strValue('PMI', 'START_TIME')
    END_TIME = config.get_strValue('PMI', 'END_TIME')
    USER_NAME = config.get_strValue('PMI', 'USER_NAME')
    PASSWORD = config.get_strValue('PMI', 'PASSWORD')
    FIRST_NAME = config.get_strValue('CN-PMI', 'FIRST_NAME')
    LAST_NAME = config.get_strValue('CN-PMI', 'LAST_NAME')
    logger.info('开始访问首页。。。。。。。%s', 'http://exam.chinapmp.cn/')
    tool = BeautySoupTool.BeautySoupTool("http://exam.chinapmp.cn/")
    while tool.error is True or tool.status_code != 200:
        logger.info(tool.status_code)
        logger.info('访问失败，重新访问。。。')
        tool = BeautySoupTool.BeautySoupTool("http://exam.chinapmp.cn/")
    # 开始登录
    logger.info('访问成功，开始登录。。。。。。。%s',
                'http://exam.chinapmp.cn/App_Ajax/SHOW.Ajax.Exam.Login,SHOW.Ajax.ajax?id =3&doing=view&domain=exam')
    login = do_login()
    while login.status_code != 200:
        logger.error('---登录失败，尝试重新登录......')
        login = do_login()
    logger.info('登录成功，访问报名页面中。。。。')
    register = do_register()
    while register.status_code != 200:
        logger.info('访问报名页面失败，重新访问中。。。。')
        register = do_register()
    logger.info('访问报名页面成功，开始报名。。。')
    response = post_data()
    while response.status_code != 200:
        logger.info('报名失败，重新报名中。。。。')
        response = post_data()
